package com.project.controller.web;

import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.myBeans.ExerciseDetail;
import com.project.myBeans.R;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping("/composePaper")
@Api(description = "组卷接口", value = "composePaper")
public class ComposePaperController {

    @Autowired
    private ComposePaperService composePaperService;

    @Autowired
    private ExerciseInfoService exerciseInfoService;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private TrueFalseService trueFalseService;

    @Autowired
    private CompletionService completionService;

    @Autowired
    private CompAnswerService compAnswerService;

    @Autowired
    private ShortAnswerService shortAnswerService;

    @Autowired
    private ProgrammingService programmingService;

    @Autowired
    private AnswerRecordService answerRecordService;

    @Autowired
    private ExerciseRecordService exerciseRecordService;

    @Autowired
    private ExerciseService exerciseService;


    /**
     * wqd 2018/5/2
     * @param composePaper
     * @return
     */
    @ApiOperation("添加题目")
    @PostMapping("/add")
    public R add(@RequestBody ComposePaper composePaper) throws Exception{
        try{
            if(composePaperService.findByPaperIdAndExerinfoid(composePaper.getPaperid(),composePaper.getExerinfoid()).isEmpty()){
                composePaperService.add(composePaper);
                return R.ok("添加成功");
            }
            else{
                return R.error("已存在");
            }
        }catch(Exception e){
            e.printStackTrace();
            return R.error("添加失败");
        }
    }


    @ApiOperation("通过试卷id查找所有题目")
    @PostMapping("/findByPaperid/{paperid}")
    public R findByCondition(@PathVariable Integer paperid) {
        try {
            List<ComposePaper> composePapers=composePaperService.findByPaperId(paperid);
            List<ExerciseDetail> exerciseDetailList = new ArrayList<>();
            for(ComposePaper composePaper:composePapers) {
                ExerciseDetail exerciseDetail = new ExerciseDetail();
                exerciseDetail.setExerciseInfo(exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid()));
//                exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid());
                if(exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid())!=null){
                switch (exerciseInfoService.findByPrimaryKey(composePaper.getExerinfoid()).getType()) {
                    case 1: //单选
                    case 2: //多选
                        exerciseDetail.setChoice(choiceService.findByExerciseInfoId(composePaper.getExerinfoid()));
                        break;
                    case 3:
                        exerciseDetail.setTrueFalse(trueFalseService.findByExerciseInfoId(composePaper.getExerinfoid()));
                        break;
                    case 4:
                        Completion completion = completionService.findByExerciseInfoId(composePaper.getExerinfoid());
                        String[] strs = compAnswerService.findByCompidOrderByIndex(completion.getId());
                        completion.setAnswerArray(strs);
                        completion.setBlankCount(strs.length);
                        exerciseDetail.setCompletion(completion);
                        break;
                    case 5:
                        exerciseDetail.setShortAnswer(shortAnswerService.findByExcerciseInfoId(composePaper.getExerinfoid()));
                        break;
                    case 6:
                        exerciseDetail.setProgramming(programmingService.findByExerciseInfoId(composePaper.getExerinfoid()));
                        break;
                }
            }
                exerciseDetailList.add(exerciseDetail);
            }
            return R.ok().put("data", exerciseDetailList).put("total", MyPageHelper.getTotal(composePapers));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    /**
     * @author wwh
     * @date 2018/5/9
     * @function 组卷时先添加一个题目，再将此题添加至试卷中
     * @param exerciseDetail
     * @return
     */
    @ApiOperation("先添加一题目，再加入试卷中")
    @PostMapping("/addExer")
    public R addExerandCompose(@RequestBody ExerciseDetail exerciseDetail,@RequestBody ComposePaper composePaper){
        try{
            exerciseService.add(exerciseDetail);
            composePaper.setExerinfoid(exerciseDetail.getExerciseInfo().getId());
            composePaperService.add(composePaper);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * Wqd 2018/5/14
     * @param
     * @return
     */
    @ApiOperation("通过paperid和exerinfoid删除题目")
    @GetMapping("/remove/{paperid}/{exerinfoid}")
    public R removeOne(@PathVariable Integer paperid,@PathVariable Integer exerinfoid){
        try{
            composePaperService.remove(paperid,exerinfoid);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

}